package sliver.action.shop.ajax;

import java.io.Writer;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import sliver.entity.Custom;
import sliver.entity.ShopVip;
import sliver.service.shop.custom.CustomService;
import sliver.service.shop.vip.ShopVipService;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionSupport;

@Scope("prototype")
@Controller("shop.ajax.customLoginAction")
public class CustomLoginAction extends ActionSupport{

	private static final long serialVersionUID = 4087141019489601183L;
	
	private String username;
	private String password;
	private Integer shopperId;
	
	@Autowired
	private CustomService customService;
	
	@Autowired
	private ShopVipService shopVipService;
	
	public CustomLoginAction(){
		
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	public Integer getShopperId() {
		return shopperId;
	}

	public void setShopperId(Integer shopperId) {
		this.shopperId = shopperId;
	}

	@Override
	public String execute() throws Exception {
		
		Custom custom = customService.customLogin(username, password);
		
		ShopVip shopVip = shopVipService.vipLogin(username, password, shopperId);
		
		StringBuilder json = new StringBuilder();
		
		if(custom != null){
			if(shopVip != null){
				json.append("{")
					.append(JSON.toJSONString("id")).append(":").append(JSON.toJSONString(custom.getId())).append(",")
					.append(JSON.toJSONString("balance")).append(":").append(JSON.toJSONString(custom.getBalance())).append(",")
					.append(JSON.toJSONString("vipLevel")).append(":").append(JSON.toJSONString(shopVip.getVip().getVipLevel())).append(",")
					.append(JSON.toJSONString("discount")).append(":").append(JSON.toJSONString(shopVip.getVip().getDiscount()))
					.append("}");
			}else{
				json.append("{")
					.append(JSON.toJSONString("id")).append(":").append(JSON.toJSONString(custom.getId())).append(",")
					.append(JSON.toJSONString("balance")).append(":").append(JSON.toJSONString(custom.getBalance())).append(",")
					.append(JSON.toJSONString("vipLevel")).append(":").append(JSON.toJSONString(0)).append(",")
					.append(JSON.toJSONString("discount")).append(":").append(JSON.toJSONString(1))
					.append("}");
			}
		}else{
			json.append("{}");
		}
		
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json;charset=utf-8");
		
		Writer writer = response.getWriter();
		writer.write(json.toString());
		writer.flush();
		
		return null;
	}
}
